//
//  PPlayController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-06-02.
//

import UIKit
import AVKit

class PPlayController: CJBaseBackController {
    
    var window: UIWindow!
    var playerView: CJPlayerView!
    
    var device: CJOrientDevice = CJOrientDevice().deviceOrientation { orient in
        kCJLog(orient)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //setupAVPlayerVC()
        //setupAVPlayer()
        
        setupCJPlayer()
        
        
    }
    
    func setupCJPlayer() {
        self.playerView = CJPlayerView(frame: CGRect(x: 0, y: 50,
                                                       width: self.view.cj_width,
                                                       height: 500))
        self.view.addSubview(self.playerView)
        let config = CJPlayerConfig()
        config.url = "https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4"
        self.playerView.config = config
        self.playerView.setupConfig()
    }
    
    func setupWindow() {
        window = UIWindow(frame: CJScreen.bounds)
        window.windowLevel = .statusBar
        window.rootViewController = UIViewController()
        window.backgroundColor = .black
        window.isHidden = false
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        //CJCommonScreen().switchScreenOrientation(vc: self, orientation: .land)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //CJCommonScreen().switchScreenOrientation(vc: self, orientation: .auto)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //CJCommonScreen().switchScreenOrientation(vc: self, orientation: .port)
    }
    
    func setupAVPlayer() {
        
        let playerItem = AVPlayerItem(url: URL(string: "https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4")!)
        let player = AVPlayer(playerItem: playerItem)
        
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = .resizeAspect
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        player.play()
        
    }
    
    func setupAVPlayerVC() {
        
        let playerItem = AVPlayerItem(url: URL(string: "https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4")!)
        let player = AVPlayer(playerItem: playerItem)
        
        let playerVC = AVPlayerViewController()
        playerVC.videoGravity = .resizeAspect
        playerVC.player = player
        playerVC.player?.play()
        
        self.addChild(playerVC)
        
        // playerVC.view.frame = CGRect(x: 0, y: 200, width: self.view.cj_width, height: 500)
        self.view.addSubview(playerVC.view)
        
        //self.navigationController?.pushViewController(playerVC, animated: true)
        //self.present(playerVC, animated: true)
        
    }

}
